package com.leetcode;

/**
 * 80. 删除有序数组中的重复项 II
 *
 * @author fy
 * @date 2022/4/1 22:33
 */
public class Solution80 {

    public int removeDuplicates(int[] nums) {
        int n = nums.length;
        int cur = nums[0];
        int j = 1;
        int count = 1;
        for (int i = 1; i < n; i++) {
            if (nums[i] != cur) {
                cur = nums[i];
                nums[j++] = nums[i];
                count = 1;
            } else if (nums[i] == cur && count > 0) {
                nums[j++] = nums[i];
                count--;
            } else {
                count--;
            }
        }
        return j;
    }

    public int removeDuplicates1(int[] nums) {
        int u = 0;
        for (int x : nums) {
            if (u < 2 || nums[u - 2] != x) {
                nums[u++] = x;
            }
        }
        return u;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{0, 0, 1, 1, 1, 1, 2, 3, 3};

        new Solution80().removeDuplicates(nums);
        for (int i = 0; i < nums.length; i++) {
            System.out.print(i + " ");
        }
    }

}
